package com.javalong.affairs.module.renwujinzhang.activity;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.javalong.affairs.R;
import com.javalong.affairs.base.common.UIStatus;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.DefaultResponseListener;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.renwujinzhang.model.Task;
import com.javalong.affairs.module.renwujinzhang.ui.SelectTimeView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectUserView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审核任务
 */
public class AuditTaskActivity extends RecycleListActivity {

    private Task task;

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, AuditTaskActivity.class);
        context.startActivity(intent);
    }

    public static void toThisActivity(Context context, Task task) {
        Intent intent = new Intent();
        intent.setClass(context, AuditTaskActivity.class);
        intent.putExtra(BundleKey.TASK, task);
        context.startActivity(intent);
    }

    @Override
    public void afterInit() {
        getSupportActionBar().setTitle("任务详情");
        task = (Task) getIntent().getSerializableExtra(BundleKey.TASK);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TIME:
                return new ViewHolder(new SelectTimeView(this));
            case MULTI_USER:
                return new ViewHolder(new SelectUserView(this));
            case EDIT:
                return new ViewHolder(mInflater.inflate(R.layout.item_imageedit, parent, false));
            case BUTTON:
                return new ViewHolder(mInflater.inflate(R.layout.item_button, parent, false));
        }
        return new ViewHolder(mInflater.inflate(R.layout.item_imageedit, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        switch (position) {
            case 0: {
                EditText editText = holder.getView(R.id.et_right);
                editText.setEnabled(false);
                editText.setHint(task.getTitle());
            }
            break;
            case 1: {
                EditText editText = holder.getView(R.id.et_right);
                editText.setEnabled(false);
                editText.setHint(task.getContent());
            }
            break;
            case 2: {
                holder.itemView.setEnabled(false);
                ((SelectTimeView) holder.itemView).setHintText(TimeUtils.getFormatTime2(task.getTimeBegin()));
                ((SelectTimeView) holder.itemView).setStatus(UIStatus.WATCH);
            }
            break;
            case 3: {
                holder.itemView.setEnabled(false);
                ((SelectTimeView) holder.itemView).setHintText(TimeUtils.getFormatTime2(task.getTimeEnd()));
                ((SelectTimeView) holder.itemView).setStatus(UIStatus.WATCH);
            }
            break;
            case 4: {
                holder.itemView.setEnabled(false);
                ((SelectUserView) holder.itemView).setHintText(task.getReceiveName());
                ((SelectUserView) holder.itemView).setStatus(UIStatus.WATCH);
            }
            break;
            case 5: {
                Button button = holder.getView(R.id.bt_item);
                button.setText("审核通过");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        auditTask(1);
                    }
                });
                break;
            }
            case 6: {
                Button button = holder.getView(R.id.bt_item);
                button.setText("审核不通过");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        auditTask(0);
                    }
                });
            }
            break;
        }
    }

    private void auditTask(int pass) {
        String url;
        if (pass == 0) {
            url = UrlUtils.getUrl("Task", "action_audit_disagree");
        } else {
            url = UrlUtils.getUrl("Task", "action_audit_agree");
        }
        Map<String, Object> param = new HashMap<>();
        param.put(RequestKey.ID, task.getId());
        HttpUtils.post(url, param, new DefaultResponseListener() {
            @Override
            public void onResponse(String jsonStr) {
                super.onResponse(jsonStr);
                finish();
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return EDIT;
            case 1:
                return EDIT;
            case 2:
                return TIME;
            case 3:
                return TIME;
            case 4:
                return MULTI_USER;
            case 5:
                return BUTTON;
            case 6:
                return BUTTON;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return 7;
    }

}
